package dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PrelevanceDAO extends BaseHibernateDAO {
    private static final Logger log = LoggerFactory
	    .getLogger(KnowledgepointDAO.class);

    // property constants
    public boolean save(Prelevance prelevance) {
	return (save(log, "saving Prelevance instance", prelevance));
    }

    public boolean add(List<Prelevance> list) {
	Session session = getSession();
	Transaction transaction = session.beginTransaction();
	try {
	    String queryString = "select prelevanceid from Prelevance where knowledgeid=? and type=0";
	    Query queryObject = getSession().createQuery(queryString);
	    queryObject.setInteger(0, list.get(0).getKnowledgeid());
	    Object object = queryObject.uniqueResult();
	    int res = -1;
	    int i = 0;
	    if (object != null) {
		res = (Integer) object;
		i = 1;
	    } else {
		queryString = "select max(prelevanceid) from Prelevance where type=0";
		queryObject = getSession().createQuery(queryString);
		object = queryObject.uniqueResult();
		res = 1;
		if (object != null) {
		    res = (Integer) object + 1;
		}
	    }
	    for (;i<list.size();i++) {		
		list.get(i).setPrelevanceid(res);
		session.save(list.get(i));
	    }
	    transaction.commit();
	    log.debug("save successful");
	    return true;
	} catch (RuntimeException re) {
	    transaction.rollback();
	    log.error("save failed", re);
	    throw re;
	}
    }

    public int delete(Prelevance prelevance) {
	try {
	    String queryString = "delete from prelevance where prelevanceid=?";
	    Query queryObject = getSession().createQuery(queryString);
	    queryObject.setParameter(0, prelevance.getPrelevanceid());
	    return queryObject.executeUpdate();
	} catch (RuntimeException re) {
	    throw re;
	}
    }

    public int deleteById(Prelevance prelevance) {
	try {
	    String queryString = "delete from prelevance where id=?";
	    Query queryObject = getSession().createQuery(queryString);
	    queryObject.setParameter(0, prelevance.getId());
	    return queryObject.executeUpdate();
	} catch (RuntimeException re) {
	    throw re;
	}
    }

    public Prelevance findById(java.lang.Integer id) {
	log.debug("getting Prelevance instance with id: " + id);
	try {
	    Prelevance instance = (Prelevance) getSession().get(
		    "dao.Prelevance", id);
	    return instance;
	} catch (RuntimeException re) {
	    log.error("get failed", re);
	    throw re;
	}
    }

    public List<Prelevance> findByProperty(String propertyName, Object value) {
	log.debug("finding Prelevance instance with property: " + propertyName
		+ ", value: " + value);
	try {
	    String queryString = "from Prelevance as model where model."
		    + propertyName + "= ?";
	    Query queryObject = getSession().createQuery(queryString);
	    queryObject.setParameter(0, value);
	    return queryObject.list();
	} catch (RuntimeException re) {
	    log.error("find by property name failed", re);
	    throw re;
	}
    }

    public List<Prelevance> findPrelevances(String prelevanceid) {
	try {
	    String queryString = "from Prelevance where prelevanceid=? and type=1";
	    Query queryObject = getSession().createQuery(queryString);
	    queryObject.setParameter(0, prelevanceid);
	    return queryObject.list();
	} catch (RuntimeException re) {
	    throw re;
	}
    }

    public List<Prelevance> findPrelevancesByKnowledge(int knowledgeid) {
	try {
	    String queryString = "from Prelevance where prelevanceid=(select prelevanceid from Prelevance where knowledgeid=? and type=0) and type=1";
	    Query queryObject = getSession().createQuery(queryString);
	    queryObject.setParameter(0, knowledgeid);
	    return queryObject.list();
	} catch (RuntimeException re) {
	    throw re;
	}
    }

    public List<Knowledgepoint> findKnowledgesByKnowledgeId(int knowledgeid) {
	try {
	    String queryString = "from Knowledgepoint where knowledgeid in(select knowledgeid from Prelevance where prelevanceid=(select prelevanceid from Prelevance where knowledgeid=? and type=0) and type=1)";
	    Query queryObject = getSession().createQuery(queryString);
	    queryObject.setParameter(0, knowledgeid);
	    return queryObject.list();
	} catch (RuntimeException re) {
	    throw re;
	}
    }

    public List<Integer> findKnowledgesByKnowledgeIds(List<Integer> knowledgeids) {
	try {
	    StringBuilder queryString = new StringBuilder(
		    "select distinct(knowledgeid) from Prelevance where prelevanceid in(select prelevanceid from Prelevance where knowledgeid in(");
	    for (int id : knowledgeids) {
		queryString.append(id).append(",");
	    }
	    queryString.replace(queryString.length() - 1, queryString.length(),
		    ")");
	    queryString.append(" and type=0) and type=1");
	    Query queryObject = getSession()
		    .createQuery(queryString.toString());
	    return queryObject.list();
	} catch (RuntimeException re) {
	    throw re;
	}
    }

    public int getPrelevanceid() {
	try {
	    String queryString = "select max(prelevanceid) from Prelevance where type=0";
	    Query queryObject = getSession().createQuery(queryString);
	    Object object = queryObject.uniqueResult();
	    int res = 1;
	    if (object != null)
		res = (Integer) object + 1;
	    return res;
	} catch (RuntimeException re) {
	    throw re;
	}
    }

    public int getExistPrelevanceid(int knowledgeid) {
	try {
	    String queryString = "select prelevanceid from Prelevance where knowledgeid=? and type=0";
	    Query queryObject = getSession().createQuery(queryString);
	    queryObject.setInteger(0, knowledgeid);
	    Object object = queryObject.uniqueResult();
	    int res = -1;
	    if (object != null) {
		res = (Integer) object;
	    }
	    return res;
	} catch (RuntimeException re) {
	    throw re;
	}
    }

    public static void main(String[] args) {
	PrelevanceDAO prelevanceDAO = new PrelevanceDAO();
	System.out.println(prelevanceDAO.findPrelevancesByKnowledge(2).size());
    }
}
